package com.ferry.taskflow.taskFlow.nontx;

import com.ferry.taskflow.taskFlow.base.IdAble;
import com.ferry.taskflow.taskFlow.base.enumv.ExecuteType;
import com.ferry.taskflow.taskFlow.base.enumv.OperateType;
import com.ferry.taskflow.taskFlow.base.enumv.StageResult;

import java.util.List;

/**
 * Created by daiyong
 * 任务中的一个阶段
 */
public class NonTxStage extends IdAble {

	private NonTxStage pre = null;

	private List<NonTxTaskExecutor> executors = Lists.newArrayList();

	/**
	 * 忽略上一个stage超时
	 */
	private Boolean ignorePreStageTimeout = false;

	/**
	 * 忽略上一个stage异常
	 */
	private Boolean ignorePreStageException = false;

	/**
	 * 超时时间 单位秒
	 */
	private Integer timeout = -1;

	private ExecuteType executeType = ExecuteType.PARALLEL;

	private OperateType operateType = OperateType.ALL;


	private StageResult stageResult = StageResult.INIT;

	private NonTxStage() {
		super();
	}

	public NonTxStage addTask(Task task) {
		executors.add(new NonTxTaskExecutor(task));
		return this;
	}

	public List<NonTxTaskExecutor> executors() {
		return this.executors;
	}

	public NonTxStage preStage(NonTxStage pre) {
		this.pre = pre;
		return this;
	}

	public NonTxStage preStage() {
		return this.pre;
	}

	public NonTxStage ignorePreStageTimeout(boolean ignorePreStageTimeout) {
		this.ignorePreStageTimeout = ignorePreStageTimeout;
		return this;
	}

	public boolean ignorePreStageTimeout() {
		return this.ignorePreStageTimeout;
	}

	public NonTxStage ignorePreStageException(boolean ignorePreStageException) {
		this.ignorePreStageException = ignorePreStageException;
		return this;
	}

	public boolean ignorePreStageException() {
		return this.ignorePreStageException;
	}

	public NonTxStage timeout(int timeout) {
		this.timeout = timeout;
		return this;
	}

	public int timeout() {
		return this.timeout;
	}

	public boolean isAll() {
		return this.operateType == OperateType.ALL;
	}
	public boolean isAny() {
		return this.operateType == OperateType.ANY;
	}

	public NonTxStage operateType(OperateType operateType) {
		this.operateType = operateType;
		return this;
	}

	public boolean isParallel() {
		return this.executeType == ExecuteType.PARALLEL;
	}

	public boolean isTimeout() {
		return this.timeout != -1;
	}

	public NonTxStage stageResult(StageResult stageResult) {
		this.stageResult = stageResult;
		return this;
	}

	public StageResult stageResult() {
		return this.stageResult;
	}

	public static NonTxStage create() {
		return new NonTxStage();
	}
}
